var windowName = "newTask";
var Page ,imageBrowser,TaskPics
apiready = function(){
    onWindowShow(function () {
        var address = getLocData("curAddress");
        logJson(address)
        if(isTrue(address)){Page.address = address};
        var contact = getLocData("curContact");
        if(isTrue(contact)){Page.contact = contact};
    })
    onWindowHide(function () {
    })
    console.log("index page is OK");
    // LiveList = new List();
    TaskPics = new PicUp()
    imageBrowser = api.require('imageBrowser')
    Page = createVM("#Page",{
        //页面数据
        lactiveStep:0
        ,location:location.href
        ,cats: []
        ,cat1:""
        ,cat1Id:0
        ,catName1:"家庭保洁"
        ,cat2:""
        ,cat2Id:0
        ,catName2:"厨房清扫"
        // ,date:new Date()
        // ,time:new Date()
        ,bookAt:new Date()
        ,callBefore:30
        ,expireAt:30
        ,title:""
        ,tips:""
        ,address:{}
        ,contact:{}
        ,pics: []
        ,picsV: []
        // ,cur:''
        ,province:""
        ,city:""
        ,area:""
        ,isInsurance:false

        //省市县去选择
        ,orgData:[]
        ,pro:{}
        ,proIndex:0
        ,cit:{}
        ,citIndex:0
        ,are:{}
        ,areIndex:0
        ,pcaStep:1
        ,pcaShow:false
    },{
        //页面方法
        submit: function () {
            if (!isTrue(this.cat2)) {showToast("请选择分类");return}
            if (!isTrue(this.title)) {showToast("请输入任务标题");return}
            if (!isTrue(this.address)) {showToast("请选择服务地址");return}
            if (!isTrue(this.contact)) {showToast("请选择联系人");return}

            var data = {
                cat1:this.cat1Id
                ,cat2:this.cat2Id
                ,bookAt:this.bookAt
                ,callBefore:this.callBefore
                ,expireAt:this.expireAt
                ,title:this.title
                ,tips:this.tips
                ,address:this.address.id
                ,contact:this.contact.id
                ,pics:this.pics
                ,favorProvince:this.province
                ,favorCity:this.city
                ,favorArea:this.area
                ,isInsurance:this.isInsurance
            }
            apiPost("/api/Tasks", data, function (res, code) {
                logJson(res)

            });

        },
        "init":function () {
            var data =getData4Page()
            // console.log(data.text);
            this.cats = getLocData("cacheCats")
            this.orgData = citys;
        }
        , "setCat1": function(index,item) {
            logJson(item)
            this.cat1 = index
            this.cat1Id = index
            this.catName1 = item.name
            //重置下级分类
            this.cat2 =""
            this.catName2 = ""
        }
        , "setCat2": function(index,item) {
            logJson(item.name)
            this.cat2 = this.cat1 + "_" + index
            this.cat2Id = index
            this.catName2 = item.name
            // showToast("请选择预约时间")
            Page.lactiveStep++;
            // setTimeout(function () {
            //
            // },300)
        }
        //图片选择器相关
        ,selectPic:function () {
            var Page = this;
            TaskPics.select(9).setCbFunc(function (pics) {
                logJson(pics,"pics")
                Page.picsV = TaskPics.getViewData()
                Page.pics = TaskPics.getData()
            })
        },
        del:function (index) {
            var pics = TaskPics.removePic(index)
            Page.picsV = TaskPics.getViewData()
            Page.pics = TaskPics.getData()
            // this.en.pics = TaskPics.getData()
        },
        move:function (index,num) {
            var pics = TaskPics.picMove(index,num)
            Page.picsV = TaskPics.getViewData()
            Page.pics = TaskPics.getData()
            // this.en.pics = TaskPics.getData()
        },
        showPic:function (index,pics) {
            imageBrowser.openImages({
                imageUrls: pics,
                activeIndex:index
            });
        }

        //地区选择器相关
        , pcaShowFunc:function () {
            this.pcaStep = 1
            this.pcaShow = !this.pcaShow
        }
        , setP: function (proIndex,pro) {
            this.pro = pro
            this.proIndex = proIndex
            this.pcaStep = 2
            //自定义行为
            this.province = pro.name
            this.city = ""
            this.area = ""
        }
        , setC: function (citIndex,cit) {
            this.cit = cit
            this.citIndex = citIndex
            this.pcaStep = 3
            //自定义行为
            this.city = cit.name
            this.area = ""
        }
        , setA: function (areIndex,are) {
            this.are = are
            this.areIndex = areIndex
            this.pcaStep = 1
            //todo 设置选择完成后的逻辑，手动关闭不算
            this.province = this.pro.name
            this.city = this.cit.name
            this.area = this.are.name
            this.code = this.are.id
            this.pcaShow = false;
            //自定义行为
            this.area = are.name
        }



        ,vhandleNext () {
            this.lactiveStep++;
        },
        vhandlePrev () {
            this.lactiveStep--;
        },
        vreset () {
            this.lactiveStep = 0;
            showConfirm()
        }
        ,changeStep: function (index) {
            this.lactiveStep = index
        }

    })

    Page.init()
}
